<template>
  <div class="fmbox">
    <div class="banner">
      <el-carousel :interval="4000" type="card" :height="bannerHeight + 'px'">
        <el-carousel-item
          v-for="(item, index) in banners"
          :key="index"
          style="textAlign:center"
        >
          <img :src="item.imageUrl" />
        </el-carousel-item>
      </el-carousel>
    </div>
    <div class="recommended">
      <h3>推荐歌单</h3>
      <div class="recombox">
        <div
          class="recom-item"
          v-for="(item, index) in recommends"
          :key="index"
        >
          <div class="imgbox" @click="golist(item.id)">
            <div class="dsc">{{ item.copywriter }}</div>
            <img :src="item.picUrl" alt />
            <span class="iconfont icon-play"></span>
          </div>
          <p>{{ item.name }}</p>
        </div>
      </div>
    </div>
    <div class="single">
      <h3>最新单曲</h3>
      <div class="singlebox">
        <div
          class="single-item"
          v-for="(item, index) in singles"
          :key="index"
          @click="onPaly(item)"
        >
          <div class="single-left">
            <img :src="item.picUrl" alt />
            <div class="iconbox">
              <span class="iconfont icon-play"></span>
            </div>
          </div>
          <div class="single-right">
            <p>{{ item.name }}</p>
            <span>{{ item.song.artists[0].name }}</span>
          </div>
        </div>
      </div>
    </div>
    <div class="mv">
      <h3>推荐MV</h3>
      <div class="mvbox">
        <div
          class="mv-item"
          v-for="(item, index) in mvs"
          :key="index"
          @click="toMv(item.id)"
        >
          <div class="mv-img">
            <img :src="item.picUrl" alt />
            <div class="iconbox">
              <span class="iconfont icon-play"></span>
            </div>
            <div class="views">
              <span class="iconfont icon-play"></span>
              <span>{{ item.playCount }}</span>
            </div>
          </div>
          <p>{{ item.name }}</p>
          <span>{{ item.artists[0].name }}</span>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {
  GetBanner,
  GetRecommend,
  GetSingle,
  GetMv,
} from "../../../../api/music"
export default {
  name: "Recommend",
  data() {
    return {
      // 图片父容器高度
      bannerHeight: 1000,
      // 浏览器宽度
      screenWidth: 0,
      loading: true,
      banners: [],
      recommends: [],
      singles: [],
      mvs: [],
    }
  },
  created() {
    this.getbanner()
    this.getrecommend()
    this.getsingle()
    this.getmv()
    this.$electron.ipcRenderer.on("resizeEvent", (e, obj) => {
      let { width } = obj
      this.screenWidth = width - 200
    })
  },
  mounted() {
    // 首次加载时,需要调用一次
    this.screenWidth = window.innerWidth
    this.setSize()
  },
  methods: {
    // 跳转到mv详情页
    toMv(id) {
      this.$router.push(`/musichome/videoinfo?id=${id}`)
    },
    // 跳转到歌单详情页
    golist(id) {
      this.$router.push(`/musichome/listinfo?id=${id}`)
    },
    // 获取banner
    async getbanner() {
      const result = await GetBanner()
      this.banners = Object.freeze(result.banners)
    },
    // 获取推荐歌单
    async getrecommend() {
      const result = await GetRecommend({
        limit: 12,
      })
      this.recommends = Object.freeze(result.result)
    },
    // 获取单曲
    async getsingle() {
      const result = await GetSingle()
      this.singles = Object.freeze(result.result)
    },
    // 获取mv
    async getmv() {
      const result = await GetMv()
      this.mvs = Object.freeze(result.result)
    },
    // 点击播放
    onPaly(item) {
      this.$store.dispatch("getSong", { id: item.id, autoPlay: 1 })
    },
    setSize: function() {
      // 通过浏览器宽度(图片宽度)计算高度
      this.bannerHeight = (500 / 1080) * 0.6 * this.screenWidth
    },
  },
}
</script>
<style lang="scss" scoped>
h3 {
  text-align: left;
  height: 50px;
  line-height: 50px;
}
.fmbox {
  position: relative;
  width: 100%;
}
.banner {
  display: block;
  width: calc(100% - 40px);
  margin: 20px;
  img {
    width: 100%;
    height: inherit;
  }
}
.el-carousel__item {
  border-radius: 10px !important;
  overflow: hidden;
}
.recommended {
  margin: 0px 20px;
}
.recombox {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
.recom-item {
  box-sizing: border-box;
  width: 150px;
  padding: 0px 10px;
  :after {
    content: "";
    width: 150px;
  }
  .imgbox {
    width: 130px;
    height: 130px;
    background: rgb(59, 144, 214);
    border-radius: 5px;
    position: relative;
    overflow: hidden;
    img {
      width: 130px;
    }
    .dsc {
      position: absolute;
      width: 100%;
      background-color: rgba(0, 0, 0, 0.5);
      top: -50px;
      left: 0px;
      color: rgb(255, 255, 255);
      font-size: 13px;
      line-height: 25px;
      padding: 0px 5px;
      box-sizing: border-box;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
      overflow: hidden;
      transition: 0.5s;
    }
    span {
      position: absolute;
      right: 10px;
      bottom: 10px;
      color: rgb(98, 158, 218);
      width: 35px;
      height: 35px;
      background: rgba(247, 247, 247, 0.5);
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 50%;
      opacity: 0;
      transition: 0.5s;
    }
    span:before {
      transform: translateX(1px);
    }
  }
  .imgbox:hover .dsc {
    top: 0px;
  }
  .imgbox:hover span {
    opacity: 1;
  }
  .imgbox:hover {
    cursor: pointer;
  }
  p {
    margin: 0px;
    height: 30px;
    line-height: 30px;
    font-size: 13px;
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
.single {
  margin: 0px 20px;
  .singlebox {
    margin: 0px 20px;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    .single-item {
      height: 100px;
      margin-bottom: 20px;
      width: 49%;
      transition: 0.5s;
      display: flex;
      .single-left {
        width: 100px;
        height: 100px;
        overflow: hidden;
        position: relative;
        img {
          width: 100px;
        }
        .iconbox {
          position: absolute;
          top: 0px;
          left: 0px;
          height: 100%;
          width: 100%;
          background: rgba(0, 0, 0, 0.5);
          display: flex;
          justify-content: center;
          align-items: center;
          opacity: 0;
          transition: 0.5s;
          span {
            color: rgb(98, 158, 218);
            width: 35px;
            height: 35px;
            background: rgba(247, 247, 247, 0.5);
            display: flex;
            justify-content: center;
            align-items: center;
            border-radius: 50%;
          }
          span:before {
            transform: translateX(1px);
          }
        }
      }
      .single-right {
        margin-left: 10px;
        width: calc(100% - 110px);
        p {
          text-align: left;
          word-wrap: break-word;
          word-break: normal;
        }
        span {
          display: inline-block;
          width: 100%;
          margin-top: 15px;
          color: rgb(128, 125, 124);
          font-size: 13px;
          text-align: left;
        }
      }
    }
    .single-item:hover {
      cursor: pointer;
      background: rgba(221, 220, 219, 0.8);
    }
    .single-item:hover .iconbox {
      opacity: 1;
    }
  }
}
.mv {
  margin: 0px 20px;
  .mvbox {
    margin: 0px 20px;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    .mv-item {
      width: 250px;
      margin-bottom: 30px;
      .mv-img {
        width: 100%;
        position: relative;
        img {
          width: 100%;
        }
        .iconbox {
          position: absolute;
          top: 0px;
          left: 0px;
          height: 100%;
          width: 100%;
          background: rgba(0, 0, 0, 0.5);
          display: flex;
          justify-content: center;
          align-items: center;
          opacity: 0;
          transition: 0.5s;
          span {
            color: rgb(98, 158, 218);
            width: 60px;
            height: 60px;
            background: rgba(247, 247, 247, 0.5);
            display: flex;
            justify-content: center;
            align-items: center;
            border-radius: 50%;
          }
          span:before {
            font-size: 30px;
            transform: translateX(2px);
          }
        }
        .views {
          position: absolute;
          top: 10px;
          right: 10px;
          span {
            margin-left: 10px;
            color: white;
          }
        }
      }
      .mv-img:hover {
        cursor: pointer;
      }
      .mv-img:hover .iconbox {
        opacity: 1;
      }
    }
    p {
      width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      margin: 10px 0px;
    }
    span {
      font-size: 14px;
      color: rgb(143, 140, 140);
    }
  }
}
</style>
